﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Net;

namespace Jackie.Silverliight.SetpBySetp.DataAndCommunications.WebClient
{
    public partial class Client : Page
    {
        public Client()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(WebClient_Loaded);
        }

        void WebClient_Loaded(object sender, RoutedEventArgs e)
        {
            List<Book> books = new List<Book>()
                                   {
                                       new Book("Professional Asp.Net 3.5"),
                                       new Book("ASP.NET AJAX In Action"),
                                       new Book("Silverlight In Action"),
                                       new Book("Asp.Net 3.5 Unleashed"),
                                       new Book("Introduction Microsoft Asp.Net AJAX")
                                 };
            lbBooks.ItemsSource = books;
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void lbBooks_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Uri endpoint = new Uri(string.Format("http://localhost:1100/BookHandler.ashx?No={0}",
                lbBooks.SelectedIndex));
            System.Net.WebClient webClient = new System.Net.WebClient();
            webClient.DownloadStringCompleted += new System.Net.DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
            webClient.DownloadStringAsync(endpoint);
        }

        void webClient_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e)
        {
            if(e.Error==null)
                lblPrice.Text = "价格：" + e.Result;

            else
            {
                lblPrice.Text = e.Error.Message;
            }
        }

    }
}
